<template>
  <div>
    <h2>分类列表</h2>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="_id" label="id" width="180"> </el-table-column>
      <el-table-column prop="name" label="名称" width="100"> </el-table-column>
      <el-table-column prop="categoriesStr" label="类型"> </el-table-column>
      <el-table-column fixed="right" label="图片" width="100">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" style="height: 40px" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
    };
  },
  methods: {
    async getTable() {
      const res = await this.$http.get("common/heroes");
      this.tableData = res.data;
      this.tableData.map((i) => {
        i.categoriesStr = i.categories.map((item) => item.name).join(',')
        return i;
      });
    },
    handleClick(item) {
      this.$router.push({ name: "heroesCreate", params: { id: item._id } });
    },
    handleDelete(item) {
      this.$confirm(`此操作将永久删除该简历--${item.name}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$http.post("common/heroes/delete", {
            id: item._id,
          });
          res.data.success
            ? this.$message({
                type: "success",
                message: "删除成功!",
              })
            : this.$message({
                type: "error",
                message: "删除失败!",
              });
          this.getTable();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getTable();
  },
};
</script>
